public class Bilinear implements Sampling {

    public int[] getSample(ImageAsArray.ImageAsArrayHolder img, float x, float y, float param) {

	float[] upVal = new float[4];
	float[] downVal = new float[4];
	float[] val = new float[4];

	int[] intVal = new int[4];

	// Calculate values for the bilinear interpolation
	int left = (int) Math.floor(x);
	int right = left + 1;
	int up = (int) Math.floor(y);
	int down = up + 1;

	// Calculate actual sampling spots (if original values exceeded the image size
	int a_left = Math.min(left, img.width - 1);
	int a_right = Math.min(right, img.width - 1);
	int a_up = Math.min(up, img.height - 1);
	int a_down = Math.min(down, img.height - 1);

	// Make the bilinear interpolation
	for (int i = 0; i < 4; i++) {

	    upVal[i] = img.pixels_ARGB[(a_up * img.width + a_left) * 4 + i] * (1 - (x - left)) + img.pixels_ARGB[(a_up * img.width + a_right) * 4 + i] * (1 - (right - x));
	    downVal[i] = img.pixels_ARGB[(a_down * img.width + a_left) * 4 + i] * (1 - (x - left)) + img.pixels_ARGB[(a_down * img.width + a_right) * 4 + i] * (1 - (right - x));
	    val[i] = upVal[i] * (1 - (y - up)) + downVal[i] * (1 - (down - y));

	}

	for (int i = 0; i < 4; i++)
	    intVal[i] = (int) Math.round(val[i]);

	return intVal;
    }
}
